package com.liwalo.dao;

// Generated 24-ago-2013 14:46:30 by Hibernate Tools 3.6.0

import static org.hibernate.criterion.Example.create;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.liwalo.entities.Ciudad;
import com.liwalo.entities.ContactoTienda;
import com.liwalo.entities.Tienda;
import com.liwalo.entities.TiendaProducto;
import com.liwalo.utils.HibernateUtil;

/**
 * Home object for domain model class Tienda.
 * @see com.liwalo.dao.Tienda
 * @author Hibernate Tools
 */
public class TiendaDAO {

	private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
	
	private static final Log log = LogFactory.getLog(TiendaDAO.class);


	public void persist(Tienda transientInstance) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {
			session.save(transientInstance);
			tx.commit();
		} catch (RuntimeException re) {
			tx.rollback();
			re.printStackTrace();
		}
	}

	public void update(Tienda instance) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {
			session.saveOrUpdate(instance);
			tx.commit();
		} catch (RuntimeException re) {
			tx.rollback();
			throw re;
		}
	}

	public void attachClean(Tienda instance) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {
			session.lock(instance, LockMode.NONE);
			tx.commit();
		} catch (RuntimeException re) {
			tx.rollback();
			throw re;
		}
	}

	public void delete(Tienda persistentInstance) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {
			session.delete(persistentInstance);
			tx.commit();
		} catch (RuntimeException re) {
			tx.rollback();
			throw re;
		}
	}

	public Tienda merge(Tienda detachedInstance) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {
			Tienda result = (Tienda) session.merge(detachedInstance);
			tx.commit();
			return result;
		} catch (RuntimeException re) {
			tx.rollback();
			throw re;
		}
	}

	public Tienda findById(java.lang.Integer id) {
		Session session = sessionFactory.getCurrentSession();
		try {
			session.beginTransaction();
			Tienda instance = (Tienda) session.get(Tienda.class, id);
			return instance;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	public Tienda findByIdCiudad(int id) {
		List result = null;
		Transaction tx = null;
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		try{
			 tx = session.beginTransaction();
	
	         Criteria criteria = session.createCriteria(Tienda.class, "tienda")
	         .createCriteria("tienda.ciudadId" , "ciudadid")
	         .add(Restrictions.eq("tiendaid.id",id));
	         result = criteria.list();
	         tx.commit();
	
	
	         if(result != null && result.size()>0){
	             return (Tienda)result.get(0);
	         }



		} catch (RuntimeException re) {
			throw re;
		}


         return null;
	}

	@SuppressWarnings("unchecked")
	public List<Tienda> findByExample(Tienda instance) {
		Session session = sessionFactory.getCurrentSession();
		try {
			session.beginTransaction();
			Criteria crit = session.createCriteria(Tienda.class).add(create(instance));
			List<Tienda> results = (List<Tienda>) crit.list();
			return results;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	
	public List<Tienda> ListaTiendaCiudad(int ciudad) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {
			tx.begin();
			List<Tienda> results = (List<Tienda>) session
					.createCriteria(Tienda.class)
					.createAlias("ciudadId", "ciudad")
					.add(Restrictions.eq("ciudad.id", ciudad))
					.add(Restrictions.eq("estado",1))
					.addOrder( Order.asc("nombre") )
					.list();
			tx.commit();
		
				
			return results;
		} catch (Exception ex) {
			log.debug("Error cargando ListaTiendaCiudad",ex );
			if(tx!=null)tx.rollback();
			throw null;
		}
	}
	
	public List<Tienda> ListaTiendaCiudadNombre(int ciudad ,String nombre) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {
			tx.begin();
			List<Tienda> results = (List<Tienda>) session
					.createCriteria(Tienda.class)
					.createAlias("ciudadId", "ciudad")
					.add( Restrictions.like("nombre", "%"+nombre+"%") )
					.add(Restrictions.eq("estado",1))
					.addOrder( Order.asc("nombre") )
					.list();
					
		
			tx.commit();	
			return results;
		} catch (Exception ex) {
			log.debug("Error cargando ListaTiendaCiudadNombre",ex );
			if(tx!=null)tx.rollback();
			throw null;
		}
	}
	
	
	

}
